/**
 * 
 */
package com.mvc.controller.member;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mvc.controller.base.BaseController;
import com.mvc.model.base.Json;
import com.mvc.model.base.SessionInfo;
import com.mvc.model.mb.archive.BaseInfo;
import com.mvc.model.mb.archive.User;
import com.mvc.service.mb.BaseInfoServiceI;
import com.mvc.service.mb.UserService;
import com.mvc.utils.MD5Util;
import com.mvc.utils.StringUtil;

/**
 * @author qingjie
 *
 */
@Controller
@RequestMapping("/admin")
public class IndexController extends BaseController {
	
	@Autowired
	private BaseInfoServiceI baseInfoServiceI;
	@Autowired
	private UserService us;

	/**
	 * 登陆验证 or 验证后根据Idcard查询用户信息
	 * @param bi
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/login")
	public Json login(BaseInfo bi, HttpSession session){
		Json j = new Json();
		List<String> resourceList = new ArrayList<String>();
		List<BaseInfo> bInfo = baseInfoServiceI.getBaseInfo(bi.getIdcard());
		SessionInfo sessionInfo = new SessionInfo();
		if(bInfo.size() > 0){
			for(BaseInfo b : bInfo){
				if(b.getPatientPwd().equals(MD5Util.md5(bi.getPatientPwd()))){
					sessionInfo.setId(b.getId());
					sessionInfo.setName(b.getName());
					sessionInfo.setLoginname(StringUtil.replaceNumberToString(b.getIdcard(),".",6,14));
					sessionInfo.setPersonId(b.getPersonId());
					sessionInfo.setSex(ValidatorUtil.parseIntByString(b.getSex()));
					sessionInfo.setBirthdate(ValidatorUtil.sdf.format(b.getBirthDate()));
					sessionInfo.setMobile(b.getMobile());
					sessionInfo.setAddress(b.getAddress());
					sessionInfo.setShortname(b.getShortName());
					sessionInfo.setPictureUrl(b.getPictureUrl());
					sessionInfo.setNickName(b.getNickName());
					sessionInfo.setCompany(b.getCompany());
					sessionInfo.setIdcard(b.getIdcard());				
					sessionInfo.setLoginname(b.getIdcard());
					sessionInfo.setResourceList(resourceList);
					j.setObj(sessionInfo);
					j.setSuccess(true);
					j.setMsg("登陆成功！");
				}else{
					j.setMsg("密码错误！");
					j.setSuccess(false);
				}
			}
		}else{
			j.setMsg("用户不存在");
			j.setSuccess(false);
		}
		return j;
	}
	
	/**
	 * 登陆验证
	 * @param user
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/sysLogin")
	public Json sysLogin(User user){
		Json j = new Json();
		User u = us.getUser(user.getLoginname());
		if(u!=null){
			if(u.getPassword().equals(MD5Util.md5(user.getPassword()))){
				j.setObj(u);
				j.setSuccess(true);
				j.setMsg("登陆成功！");
			}else{
				j.setMsg("密码错误！");
				j.setSuccess(false);
			}
		}else{
			j.setMsg("用户不存在");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping("/findBaseInfoByPersonId")
	@ResponseBody
	public Json findBaseInfoByPersonId(HttpServletRequest request){
		String personIds = request.getParameter("personIds");
		List<BaseInfo> list = baseInfoServiceI.getBaseInfos(personIds);
		Json j = new Json();
		if(list.size()>0){
			j.setObj(list);
			j.setSuccess(true);
			j.setMsg("查询成功！");
		}else{
			j.setMsg("查询失败！");
			j.setSuccess(false);
		}
		return j;
	}
	
	/**
	 * @author qingjie
	 * 根据不同条件获取基本信息
	 * @param bi
	 * @return
	 */
	@RequestMapping("/findBaseInfo")
	@ResponseBody
	public List<BaseInfo> findBaseInfo(BaseInfo bi){
		return baseInfoServiceI.getBaseInfo(bi.getIdcard());
	}
}
